/*
 * Copyright 2025 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

import ".."
import "../configitem"
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Window 2.15
import org.ukui.quick.items 1.0 as UkuiItems
import org.ukui.quick.platform 1.0

Window {
    id: window

    property int dragAreaHeight: 40
    property int itemWidth: 480
    property int itemHeight: 435
    property int itemCheckBoxHeight: 20
    property int itemLineEditHeight: 36
    property int itemSwitchHeight: 24
    property int itemSpacing: 16
    property int dragX: 0
    property int dragY: 0
    property bool dragging: false
    property bool isCentered: true
    property bool enableBackground: false
    property string title: ""

    function restoreAction() {
        configManager.restoreDefaultImConfig();
    }

    function cancelAction() {
        imConfigPage.configGroups = null;
        configManager.releaseImConfigResource();
        window.destroy();
    }

    function saveAction() {
        configManager.saveImConfig();
        imConfigPage.configGroups = null;
        configManager.releaseImConfigResource();
        window.destroy();
    }

    function handleDragStarted(posX, posY) {
        dragX = posX;
        dragY = posY;
        dragging = true;
    }

    function handleDragging(posX, posY) {
        if (dragging) {
            window.x += posX - dragX;
            window.y += posY - dragY;
        }
    }

    function handleCloseClicked() {
        imConfigPage.configGroups = null;
        configManager.releaseImConfigResource();
        window.destroy();
    }

    function handleDragEnd() {
        dragging = false;
    }

    width: 480
    height: 560
    modality: Qt.ApplicationModal
    flags: Qt.FramelessWindowHint
    color: "transparent"

    Connections {
        function onImConfigurationChanged() {
        }

        target: configManager
    }

    UkuiItems.Background {
        id: imBackGround

        anchors.fill: parent
        color: Theme.base()
        radius: 6
        clip: true
        Theme.onPaletteChanged: {
            color = Theme.base();
        }
        border.width: 1
        border.color: Qt.rgba(0, 0, 0, 0.1)

        CustomHeader {
            id: customHeader

            width: parent.width
            height: dragAreaHeight
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.leftMargin: 8
            anchors.rightMargin: 4
            headerTitle: qsTr(title) + "-" + qsTr("Config")
            z: 1
            onDragStarted: {
                handleDragStarted(posX, posY);
            }
            onDragging: {
                handleDragging(posX, posY);
            }
            onDragEnded: {
                handleDragEnd();
            }
            onCloseClicked: {
                handleCloseClicked();
            }
        }

        ConfigPageTemplate {
            id: imConfigPage

            function openExternalConfig(uri, name) {
                configManager.getExternalConfig(uri);
                var externalSubConfig = externalComponent.createObject(parent);
                externalSubConfig.title = name;
                externalSubConfig.show();
            }

            anchors.top: customHeader.bottom
            anchors.left: parent.left
            anchors.right: parent.right
            height: 435
            width: window.width
            itemSpacing: window.itemSpacing
            itemCheckBoxHeight: window.itemCheckBoxHeight
            itemLineEditHeight: window.itemLineEditHeight
            itemSwitchHeight: window.itemSwitchHeight
            autoSave: false
            enableBackground: window.enableBackground
            groupTitlebold: false
            configGroups: configManager.imConfig
            openExternalFunction: openExternalConfig
        }

        UkuiItems.Background {
            anchors.top: imConfigPage.bottom
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.leftMargin: imBackGround.border.width
            anchors.rightMargin: imBackGround.border.width
            height: 1
            Layout.preferredHeight: 1
            color: Theme.button()
            Theme.onPaletteChanged: {
                color = Theme.button();
            }
        }

        RowLayout {
            anchors.top: imConfigPage.bottom
            anchors.topMargin: 16
            width: parent.width
            spacing: 8
            visible: !window.autoSave

            CustomButton {
                width: 88
                height: 36
                Layout.leftMargin: 24
                Layout.alignment: Qt.AlignLeft
                labelText: qsTr("Restore")
                onCustomButtonClicked: restoreAction()
            }

            Item {
                Layout.fillWidth: true
            }

            CustomButton {
                width: 88
                height: 36
                labelText: qsTr("Cancel")
                Layout.alignment: Qt.AlignRight
                onCustomButtonClicked: cancelAction()
            }

            CustomButton {
                width: 88
                height: 36
                enableHighlight: true
                labelText: qsTr("OK")
                Layout.rightMargin: 24
                Layout.alignment: Qt.AlignRight
                onCustomButtonClicked: saveAction()
            }

        }

    }

    Component {
        id: externalComponent

        ExternalSubConfig {
            id: externalSubConfig
        }

    }

}
